from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import os
import sys
from ui1 import Ui_Form
import car_ocr
##########################################
path="./1.png"
#ui界面设置
class Ui_MainWindow(object):

    def setupUi(self, MainWindow):

        #主窗口参数设置
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 300)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("车牌识别")

        # 设置按键参数
        self.file = QtWidgets.QPushButton(self.centralwidget)
        self.file.setGeometry(QtCore.QRect(20, 30, 150, 30))
        self.file.setObjectName("打开文件")

        # 设置按键参数
        self.ocr = QtWidgets.QPushButton(self.centralwidget)
        self.ocr.setGeometry(QtCore.QRect(20, 100, 150, 30))
        self.ocr.setObjectName("识别车牌")

        # 设置按键参数
        self.cmr = QtWidgets.QPushButton(self.centralwidget)
        self.cmr.setGeometry(QtCore.QRect(20, 170, 150, 30))
        self.cmr.setObjectName("打开摄像头")

        # 设置显示窗口参数
        self.fileT = QtWidgets.QLabel(self.centralwidget)
        self.fileT.setGeometry(QtCore.QRect(200, 30, 450, 31))
        self.fileT.setObjectName("文件路径")

        # 设置显示窗口参数
        self.ts = QtWidgets.QLabel(self.centralwidget)
        self.ts.setGeometry(QtCore.QRect(200, 170, 450, 31))
        self.ts.setObjectName("提示")


        #主窗口及菜单栏标题栏设置
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        ################button按钮点击事件回调函数################

        self.file.clicked.connect(self.msg)
        self.ocr.clicked.connect(self.to_Ocr)
        self.cmr.clicked.connect(self.to_Cmr)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "车牌识别"))
        self.file.setText(_translate("MainWindow", "选择文件"))
        self.ocr.setText(_translate("MainWindow", "识别"))
        self.cmr.setText(_translate("MainWindow", "打开摄像头"))
        self.fileT.setText(_translate("MainWindow", "请选择文件"))
        self.ts.setText(_translate("MainWindow", "提示：按 \"S\" 识别，按 \"Q\" 退出"))

    #########选择图片文件夹#########

    def msg(self,Filepath):
        global path
        #try:
        filename,fileType = QtWidgets.QFileDialog.getOpenFileName(None,'open file', os.getcwd(),"PNG 图片(*.png);;JPG 图片(*.jpg);;全部文件(*)")
        #except:
        #    fileType=""

        path=filename
        print(path)
        self.fileT.setText(path)

    def to_Ocr(self,Filepath):
        print("开始识别！")
        print(path)
        car_ocr.ocr_win(path)

    def to_Cmr(self,Filepath):
        print("启动摄像头！")
        try:
            car_ocr.cmr()
        except:
            print("摄像头关闭")

    # def new_window(self,str1=[]):
    #     form1=QDialog()
    #     ui = Ui_Form()
    #     ui.setupUi(form1,str1)
    #     form1.show()
    #     form1.exec_()


#########主函数入口 #########

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())
